from sanic.views import HTTPMethodView
from sanic.response import text,json
import time
from processing.other_func import ProToken,Sercret_all,Serialization,Random_id,date_format
import json as jn
import re
from processing.Sql_DataFrom import ProJosn
import requests


class other_type_add(HTTPMethodView):
    async def post(self, request):
        db = request.app.ctx.db
        # 获取用户列表
        user_id = request.app.ctx.user_id
        client_ip = request.ip

        html_data = request.json
        try:
            sql = """
                insert into other_type(user_id,type,type_title,type_image,type_cover,sort,recommend,status,ctime) VALUES(%s,%s,%s,%s,%s,%s,%s,%s,now())
            """
            db.insert(sql,(user_id,html_data['explain'],html_data['type_title'],html_data['type_image'],html_data['type_cover'],html_data['sort'],html_data['recommend'],html_data['status']))

            return json({'code': 200, 'message': "添加成功!"})

        except Exception as error:
            error_sql = 'insert into error_log(visit_ip,error_path,error_content,create_time ) VALUES (%s,%s,%s,now())'
            db.insert(error_sql, (client_ip, '/other_type/add', error))
            data = {
                'code':500,
                'message':'后端报错!'
            }
            return json(data)

class other_type_list(HTTPMethodView):
    async def get(self, request):
        db = request.app.ctx.db
        # 获取用户列表
        user_id = request.app.ctx.user_id
        client_ip = request.ip

        html_data = request.json
        try:

            sql = """
                select id,type_title,type_cover,recommend,ctime,status from other_type where user_id=%s
            """
            data = db.find_all(sql,(user_id))
            if data:
                dataList = ProJosn(data, sql, 'sql').to_dict(orient='records')
            else:
                dataList = []


            return json({'code': 200,'data':dataList, 'message': "请求成功!"})

        except Exception as error:
            error_sql = 'insert into error_log(visit_ip,error_path,error_content,create_time ) VALUES (%s,%s,%s,now())'
            db.insert(error_sql, (client_ip, '/other_type/list', error))
            data = {
                'code':500,
                'message':'后端报错!'
            }
            return json(data)
        
class other_type_del(HTTPMethodView):
    async def post(self, request):
        db = request.app.ctx.db
        # 获取用户列表
        user_id = request.app.ctx.user_id
        client_ip = request.ip

        html_data = request.json

        try:

            sql = "delete from other_type where id=%s"

            db.delete(sql, (int(html_data['id'])))

            return json({'code': 200, 'message': "删除成功!"})

        except Exception as error:
            error_sql = 'insert into error_log(visit_ip,error_path,error_content,create_time ) VALUES (%s,%s,%s,now())'
            db.insert(error_sql, (client_ip, '/other_type/delete', error))
            data = {
                'code':500,
                'message':'后端报错!'
            }
            return json(data)
